/**
 * 
 */
package leetCode;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * @author zhong
 *
 */
public class SentenceSimilarity {
	public boolean areSentencesSimilar(String[] words1, String[] words2, String[][] pairs) {
		if (words1.length != words2.length) {// 长度相等
			return false;
		}
		int n = pairs.length;
		HashMap<String, ArrayList<String>> hashMap1 = new HashMap<>(n);
		for (String[] strings : pairs) {
			addInto(hashMap1, strings[0], strings[1]);
			addInto(hashMap1, strings[1], strings[0]);
		}

		for (int i = 0; i < words1.length; i++) {
			String string = words1[i];
			if (words2[i].equals(string)) {// 相等
				continue;
			}
			// 找相似
			if (!hashMap1.containsKey(string)) {
				return false;
			}
			if (!hashMap1.get(string).contains(words2[i])) {
				return false;
			}
		}
		return true;
	}

	private void addInto(HashMap<String, ArrayList<String>> hashMap1, String key, String val) {
		if (hashMap1.containsKey(key)) {
			hashMap1.get(key).add(val);
		} else {
			ArrayList<String> strings = new ArrayList<>();
			strings.add(val);
			hashMap1.put(key, strings);
		}
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		String[] words1 = "great acting skills".split(" ");
		String[] words2 = "fine drama talent".split(" ");
		String[][] pairs = { { "great", "fine" }, { "acting", "drama" }, { "skills", "talent" } };
		System.out.println(new SentenceSimilarity().areSentencesSimilar(words1, words2, pairs));
	}

}
